$(function() {
	$('.creat').roleCreate();

});

(function($) {

	$.fn.roleCreate = function() {

		obj = $(this);
		Check.Create({
			url: obj.attr('url')
		});

		obj.on('ifChecked', 'input[type="checkbox"]', function() {
			console.log($(this).attr("type"));
			console.log($(this).attr("id"));
		})

	};
	Check = {

		//iCheck鐨刼ptions
		opts: {
			checkboxClass: 'icheckbox_square-blue',
			radioClass: 'iradio_square-blue',
			increaseArea: '20%'
		},

		Create: function(options) {

			$.get(options.url, function(data) {
				$.each(data, function(name, items) {

					//鏋勫缓panel
					var panel = $('<div>', {
						"class": "panel panel-primary",
						'id': 'panel_' + name
					}).appendTo(obj);
					//鏋勫缓panel_head
					var panel_head = $('<div>', {
						"class": "panel-heading"
					}).text(name).appendTo(panel);
					//鏋勫缓panel_body
					var panel_body = $('<div>', {
						"class": "panel-body check_ground"
					}).appendTo(panel);

					$.each(items, function(name, item) {
						var div_row = $('<div>', {
							"class": "row  check_ground col-md-6"
						}).appendTo(panel_body);

						var strong = $('<strong>', {}).text(item.name).appendTo(div_row);
						var ul = $('<ul>', {}).appendTo(div_row);
						//鏋勫缓R鐨刲i
						var li = $('<li>', {}).appendTo(ul);

						var input_R = $('<input>', {
							"type": "checkbox",
							"id": "input_" + item.moduleid + "R"
						}).appendTo(li);
						var label_R = $('<label>', {
							"for": input_R.id
						}).text("只读").appendTo(li);
						//鏋勫缓M鐨刲i
						var li = $('<li>', {}).appendTo(ul);
						var input_M = $('<input>', {
							"type": "checkbox",
							"id": "input_" + item.moduleid + "M"
						}).appendTo(li);
						var label_M = $('<label>', {
							"for": input_M.id
						}).text("修改").appendTo(li);
						//璁剧疆涓や釜li鐨刬Check
						switch(item.role) {
							case '0':
								break;
							case '1':
								$(input_R).iCheck('check');
								break;
							case '2':
								$(input_M).iCheck('check');
								break;
							default:
								break;
						}
						$(input_R).iCheck(Check.opts);
						$(input_M).iCheck(Check.opts);
					})
				})
			});
		}
	}

})(jQuery)